<?php

namespace App\Http\Controllers\Common;

use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Common\Menu;
use App\Helpers\Code;
use Illuminate\Validation\Rule;

class MenuController extends Controller
{

    public function __construct()
    {
        $this->_validations();

        $this->model = new Menu();
    }

    /**
     * 添加菜单
     * @param Request $request
     * @return array
     */
    public function addMenu(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->addMenu($data);

            $this->checked($model, ['null' => 'common.add_failure', Code::INSIDE_EXIST_DEPENDENCE => 'system.menu.menu_level_dependence', Code::INSIDE_DATABASE_ERROR => 'common.database_error']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除指定菜单
     * @param $id   菜单id
     * @return array
     */
    public function deleteMenu($id)
    {
        try {
            $model = $this->model->deleteMenu($id);

            $this->checked($model, [
                'null' => 'common.delete_failure',
                Code::INSIDE_EXIST_DEPENDENCE => 'menu.delete_failure_dependence',
                Code::INSIDE_DATABASE_ERROR => 'common.database_error',
                Code::INSIDE_ERROR => 'menu.menu_action_exist',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑菜单
     * @param $id   菜单id
     * @param Request $request
     * @return array
     */
    public function editMenu($id, Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));

            $this->rules['url'] = ['bail', 'required', 'max:200', Rule::unique('menus')->ignore($id)];

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editMenu($id, $data);

            $this->checked($model, [
                'null' => 'common.edit_failure',
                Code::INSIDE_EXIST_DEPENDENCE => 'system.menu.child_can_not_parent',
                Code::INSIDE_BEYOND_LIMIT => 'system.menu.menu_level_dependence',
                Code::INSIDE_DATABASE_ERROR => 'common.database_error',
                Code::INSIDE_ERROR => 'menu.menu_edit_error',
                Code::INSIDE_ERROR_1 => 'menu.menu_parent_menu_error'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取菜单列表
     * @param Request $request
     * @return array
     */
    public function getMenuList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);
            //单页数量
            $limit = (int) $request->input('limit');
            //排序
            $sorter = ['sort', 'asc'];

            if ($request->has('sorter')) {
                $sorter = $this->handleSort($request, $this->model->sorters);
            }

            return preprocess($this->model->getMenuList($limit, $sorter, $search));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取全部菜单列表
     * @return array
     */
    public function getMenuListAll()
    {
        try {
            $this->result['data'] = $this->model->getMenuListAll();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取菜单目录结构
     * @return array
     */
    public function getMenuTree()
    {
        try {
            $this->result['data'] = $this->model->getMenuTree();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取菜单列表
     * @param $id
     * @return array
     */
    public function getMenuChildList($id)
    {
        try {
            $model = $this->model->getMenuChildList($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取菜单
     * @param $id
     * @return array
     */
    public function getMenu($id)
    {
        try {
            $model = $this->model->getMenu($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 指定菜单下全部功能，无分页,返回全部字段
     * @param $id
     * @return array
     */
    public function getMenuActionAll($id)
    {
        try {
            $model = $this->model->getMenuActionAll($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    private function _validations()
    {
        $this->rules = [
            'parent_id' => 'nullable|integer',
            'name' => 'bail|required|max:50',
            'path' => 'bail|required|max:200',
            'url' => 'bail|required|unique:menus|max:200',
            'icon' => 'max:100',
            'target' => 'bail|nullable|in:_blank,_self,_parent,_top',
            'hide_menu' => 'boolean',
            'hide_breadcrumb' => 'boolean',
            'level' => 'bail|required|in:1,2,3',
            'sort' => 'integer',
        ];

        $this->messages = [
            'parent_id.integer' => trans('system.menu.parent_id_integer'),
            'name.required' => trans('system.menu.name_required'),
            'name.max' => trans('system.menu.name_max'),
            'path.required' => trans('system.menu.path_required'),
            'path.max' => trans('system.menu.path_max'),
            'url.required' => trans('system.menu.url_required'),
            'url.unique' => trans('system.menu.url_unique'),
            'url.max' => trans('system.menu.url_max'),
            'icon.max' => trans('system.menu.icon_max'),
            'target.in' => trans('system.menu.target_in'),
            'hide_menu.boolean' => trans('system.menu.hide_menu_boolean'),
            'hide_breadcrumb.boolean' => trans('system.menu.hide_breadcrumb_boolean'),
            'level.required' => trans('system.menu.level_required'),
            'level.in' => trans('system.menu.level_in'),
            'sort.integer' => trans('system.menu.sort_integer'),
        ];
    }

}
